package app.auto.runner.base.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.aliang.auto.R;

import java.util.Map;
import java.util.TreeMap;

import app.auto.runner.base.DipUtil;


@SuppressLint("AppCompatCustomView")
public class RoundedImageView extends ImageView {
    private int width;
    private int height;
    private Path mPath;
    private float mRadius = DipUtil.fromDip(20);
    private boolean isBottomRadius;
    public static final String NS_ANDROID = "http://schemas.android.com/apk/res/android";

    public RoundedImageView(Context context) {
        this(context, null, 0);
    }

    public RoundedImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    BitmapDrawable d;
    int srcid;

    public RoundedImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
//        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundedImageView, defStyleAttr, 0);
        isBottomRadius = false;
//        a.recycle();
        mPath = new Path();

        if (getLayoutParams() != null && getLayoutParams().width > 0 && getLayoutParams().width == getLayoutParams().height) {
            float f = getLayoutParams().width / 2f;
            mRadius = f;
        }
        srcid = attrs.getAttributeResourceValue(NS_ANDROID, "src", 0);


//        else if (attrs.getAttributeValue("", "src").contains("/mipmap")) {
//            Drawable d = getResources().getDrawable(srcid);
//
//        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        if (getLayoutParams() != null) {
            width = getLayoutParams().width;
            height = getLayoutParams().height;
            if (srcid == 0) {
                srcid = R.drawable.desktop_icon;
            }
            setImageResource(srcid);
        }
    }

    @Override
    public void setImageResource(int resId) {
        super.setImageDrawable(new RoundImageDrawable(((BitmapDrawable) getResources().getDrawable(resId)).getBitmap()).setRadius(width));
    }


    public void setRadius(float radius) {
        mRadius = radius;
    }

    static Map<Object, Float> radiusMark = new TreeMap<Object, Float>();

    static {
        radiusMark.put(RoundedImageView.class.getName(), 15F);
    }

    public RoundedImageView markRadiusIfNot(Object o, float radius) {
        if (!radiusMark.containsKey(o) && o != null) {
            radiusMark.put(o, radius);
        }
        return this;
    }

    /***
     *  default 15
     * @param o
     * @return
     */
    public RoundedImageView markRadius(Object o) {
        if (o == null || !radiusMark.containsKey(o)) {
            setRadius(radiusMark.get(RoundedImageView.class.getName()));
        } else
            setRadius(radiusMark.get(o));
        return this;
    }

    public void setBottomRadius(boolean isBottomRadius) {
        this.isBottomRadius = isBottomRadius;
    }

//    @SuppressLint("DrawAllocation")
//    @Override
//    protected void onDraw(Canvas canvas) {
//        if (mRadius > 0) {
//            mPath.reset();
//            Paint paint = new Paint();
//            paint.setShader(new BitmapShader(d.getBitmap(), BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
//            paint.setAntiAlias(true);
//            float r = width / 2f;
////            canvas.drawColor(getResources().getColor(R.color.transparent));
//            canvas.drawCircle(r, r, r, paint);
////            if (isBottomRadius) {
////                float[] rids = {0.0f, 0.0f, 0.0f, 0.0f, mRadius, mRadius, mRadius, mRadius};
////                mPath.addRoundRect(0, 0, getWidth(), getHeight(), rids, Path.Direction.CW);
////            } else {
////                mPath.addRoundRect(0, 0, getWidth(), getHeight(), mRadius, mRadius, Path.Direction.CW);
////            }
////            canvas.clipPath(mPath);
//        }
//        super.onDraw(canvas);
//    }

    public static class RoundImageDrawable extends Drawable {

        private Paint mPaint;
        private Bitmap mBitmap;
        private RectF mRectF;
        private int mRound;
        private int radius;

        public RoundImageDrawable(Bitmap bitmap) {
            this.mBitmap = bitmap;
            mPaint = new Paint();
            mPaint.setAntiAlias(true);
            BitmapShader shader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
            mPaint.setShader(shader);
        }

        /**
         * 初始化区域
         */
        @Override
        public void setBounds(int left, int top, int right, int bottom) {
            mRectF = new RectF(left, top, right, bottom);
            super.setBounds(left, top, right, bottom);
        }

        /**
         * 核心代码： 绘制圆角
         */
        @Override
        public void draw(Canvas canvas) {
            canvas.drawCircle(getIntrinsicWidth() / 2, getIntrinsicHeight() / 2, radius, mPaint);
//            canvas.drawCircle(mRectF, mRound, mRound, mPaint);
        }

        /**
         * 暴露给外面设置圆的半径
         *
         * @param radius
         */
        public RoundImageDrawable setRadius(int radius) {
            this.radius = radius;
            invalidateSelf();
            return this;
        }

        /**
         * 暴露给外面设置圆角的大小
         *
         * @param round
         */
        public RoundImageDrawable setRound(int round) {
            this.mRound = round;
            invalidateSelf();
            return this;
        }

        /**
         * getIntrinsicWidth、getIntrinsicHeight主要是为了在View使用wrap_content的时候，
         * 提供一下尺寸，默认为-1可不是我们希望的
         */
        @Override
        public int getIntrinsicHeight() {
            return mBitmap.getHeight();
        }

        @Override
        public int getIntrinsicWidth() {
            return mBitmap.getWidth();
        }

        /**
         * 根据画笔设定drawable的透明度
         */
        @Override
        public void setAlpha(int alpha) {
            mPaint.setAlpha(alpha);
        }

        /**
         * 根据画笔设定drawable的颜色过滤器
         */
        @Override
        public void setColorFilter(ColorFilter cf) {
            mPaint.setColorFilter(cf);
        }

        @Override
        public int getOpacity() {
            return PixelFormat.TRANSLUCENT;
        }

    }
}
